<?php

namespace App\Model\Member;

use App\Component\DeviceIdentity\DeviceUser;
use App\Component\User;
use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Core\Output;
use App\Util\CommonHelper;
use App\Util\UserIdentityHelper;
use EasySwoole\Component\Di;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\Redis\Redis;
use EasySwoole\RedisPool\RedisPool;

/**
 * CREATE TABLE `ss_member_profit_statistics` (
 * `id` int(11) NOT NULL AUTO_INCREMENT,
 * `date` date NOT NULL COMMENT '日期',
 * `user_id` int(11) NOT NULL COMMENT '用户id',
 * `source_id` int(11) NOT NULL COMMENT '来源id',
 * `type` char(20) NOT NULL COMMENT '变动类型 USERPROFIT:个人提成;REFFERERPROFIT:团队提成',
 * `total` int(10) NOT NULL COMMENT '数量',
 * `amount` decimal(12,2) NOT NULL COMMENT '总金额',
 * `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '创建人员账号',
 * `created_by` varchar(32) NOT NULL DEFAULT '' COMMENT '创建人员账号',
 * `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '最后更新时间',
 * `updated_by` varchar(32) NOT NULL DEFAULT '' COMMENT '最后更新人员账号',
 * `is_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除[0:未删除，1：已删除]',
 * `deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '删除时间',
 * PRIMARY KEY (`id`),
 * UNIQUE KEY `idx_unique` (`date`,`user_id`,`source_id`,`type`,`deleted_at`) USING BTREE,
 * KEY `user_id` (`user_id`),
 * KEY `source_id` (`source_id`)
 * ) ENGINE=InnoDB AUTO_INCREMENT=2174 DEFAULT CHARSET=utf8mb4 COMMENT='收益统计表';
 */
/**
 * Class MemberModel
 * @property integer $id
 * @property integer $user_id
 * @property integer $source_id
 * @property string $type
 * @property integer $total
 * @property float $amount
 * @property string $created_at
 * @property string $created_by
 * @property string $updated_at
 * @property string $updated_by
 * @property integer $is_delete
 * @property string $deleted_at
 */
class MemberProfitStatisticsModel extends BaseModel
{

    protected $tableName = 'ss_member_profit_statistics';

    protected $casts = [
        'id'         => 'integer',
        'user_id'   => 'integer',
        'source_id'  => 'integer',
        'type'      => 'string',
        'total'     => 'integer',
        'amount'    => 'float',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
        'is_delete' => 'integer',
        'created_by' => 'string',
        'updated_by' => 'string',
    ];

    const TYPE_USER_PROFIT = 'USERPROFIT';//个人提成
    const TYPE_REFFERER_PROFIT = 'REFFERERPROFIT';//团队提成
    const ARCHIVE_RECORD_LIMIT = 5000; //存档记录条数

    public static function getTypeDict(?string $key = null)
    {
        return self::getMap([
            self::TYPE_USER_PROFIT     => '个人提成',
            self::TYPE_REFFERER_PROFIT => '团队提成',
        ], $key);
    }

    protected static function onAfterUpdate($model, $res)
    {
        self::delCache('memberProfitStatisticsCache:');
    }

    protected static function onAfterInsert($model, $res)
    {
        self::delCache('memberProfitStatisticsCache:');
    }
}
